<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="roleList"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="120" />
      <el-table-column
        label="内容"
        prop="title"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column
        label="电话"
        prop="content"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['staff:post:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="联系电话" prop="content">
          <el-input 
          v-model="form.content" 
          type="number"
          oninput="if(value.length>11)value=value.slice(0,11)"
          placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAboutList, AboutInfo, updateAbout } from "@/api/zhao/phone.js";

export default {
  name: "Role",

  data() {
    var checkPhone = (rule, value, callback) => { // 手机号验证
        if (!value) {
          return callback(new Error('手机号不能为空'));
        } else {
          const reg = /^1[3456789]\d{9}$/
          if (reg.test(value)) {
            callback();
          } else {
            return callback(new Error('请输入正确的手机号'));
          }
        }
      }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "修改联系电话",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: undefined,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      rules:{
        content: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {validator: checkPhone, trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    cancel(){
        this.open = false;
    },
    submitForm() {
        console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
            updateAbout(this.form).then((res) => {
            this.open = false;
            this.getList();
          });
        }
      });
    },
    getList() {
      this.loading = true;
      getAboutList(this.queryParams).then((res) => {
        console.log(res);
        this.roleList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleUpdate(row) {
      const postId = row.id || this.ids;
      AboutInfo({ id: postId }).then((res) => {
        this.form = res.data;
        this.open = true;
      });
    },
  },
};
</script>
